#include "cairwind.h"
#include "cmd_parse/json_device_object.h"

CAirWind::CAirWind(QObject *parent) :
    CKnxControl(parent)
{
}

void CAirWind::setKnxObject(void *jOBJECT)
{
    //表达式状态
    Json_DEVICE_OBJECT *object = (Json_DEVICE_OBJECT *)jOBJECT;

    //发送值
    _actionList = object->_action.split(";");
    QStringList arrys = object->_send.split(";");

    if(arrys.size()>1 && _actionList.size() == arrys.size())
    {
        for(int i=0;i<_actionList.size();i++)
        {
            QString action = _actionList.at(i);
            if(action == "low")
                _sendLowArray = arrys.at(i).split(",");
            else if(action == "middle")
                _sendMiddleArray = arrys.at(i).split(",");
            else if(action == "high")
                _sendHightArray = arrys.at(i).split(",");
            else if(action == "auto")
                _sendAutoArray = arrys.at(i).split(",");
            else if(action == "strong")
                _sendStrongArray = arrys.at(i).split(",");
            else if(action == "powerful")
                _sendPowerfulArray = arrys.at(i).split(",");
        }
    }

    //反馈值
    arrys = object->_feedback.split(":");
    if(arrys.size() > 3)
    {
        _feedbackAddress = arrys.at(0);
        _feedbackDataType =  (DATA_TYPE)arrys.at(1).toUInt();
        _feedbackValueDefine =  (DATA_DEFINE)arrys.at(2).toUInt();
        _feedbackValueArray = arrys.at(3).split("|");
    }

}

void CAirWind::readKnxAction()
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackAddress;

    eThread->execList.append(s);

    eThread->start();
}

void CAirWind::execKnxAction(QString action,QString value)
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    QStringList *sendArray = &_sendLowArray;

    if (action == "low") {
        sendArray = &_sendLowArray;
    }
    else if (action == "middle") {
        sendArray = &_sendMiddleArray;
    }
    else if (action == "high") {
        sendArray = &_sendHightArray;
    }
    else if (action == "auto") {
        sendArray = &_sendAutoArray;
    }
    else if (action == "strong") {
        sendArray = &_sendStrongArray;
    }
    else if (action == "powerful") {
        sendArray = &_sendPowerfulArray;
    }

    QString sendAddress,sendValue;
    DATA_TYPE sendDataType;
    DATA_DEFINE sendValueDefine;

    for (int i=0; i<sendArray->size(); i++)
    {
        QString dev = sendArray->at(i);
        QStringList devArray = dev.split(":");

        if (devArray.size() > 3) {
            sendAddress = devArray.at(0);

            if (sendAddress == "msleep") {
                CExecStruct *s = new CExecStruct();
                s->address = dev;

                eThread->execList.append(s);
            }
            else
            {
                sendDataType = (DATA_TYPE)devArray.at(1).toUInt();
                sendValueDefine = (DATA_DEFINE)devArray.at(2).toUInt();
                sendValue = devArray.at(3);

                if(!sendAddress.isEmpty())
                {
                    int data_len = 0;
                    memset(_data_buf,0,sizeof(_data_buf));

                    if((data_len = DEVICE_OBJECT::KnxQStringToData(sendValue,sendDataType,sendValueDefine,(_u8 *)_data_buf)) == -1)
                    {
                        qDebug() << "SetDev ret='-1' msg='error data args'" ;
                        continue ;
                    }

                    CExecStruct *s = new CExecStruct();
                    s->address = sendAddress;
                    s->type = sendDataType;
                    s->data = QByteArray((const char *)_data_buf,data_len);

                    eThread->execList.append(s);
                }
            }
        }

    }

    eThread->start();

}

QString CAirWind::actionFromStatus(QString address,QString value)
{
    int index = _feedbackValueArray.indexOf(value);
    if(index>=0 && _actionList.size()>index)
    {
        return _actionList.at(index);
    }

    return "unkown";
}

void CAirWind::execDeviceAction(QString action,QString value,QString unit)
{
    if("low" == action || "0" == action){
        action = "low";
    }
    else if("meadian" == action || "1" == action){
        action = "middle";
    }
    else if("high" == action || "2" == action){
        action = "high";
    }
    else if("higher" == action || "3" == action){
        action = "strong";
    }
    else if("highest" == action || "4" == action){
        action = "powerful";
    }
    else if("auto" == action || "10" == action){
        action = "auto";
    }

    execKnxAction(action,value);
}
